local skel = fk.CreateSkill {
  name = "emo__qingyan",
}

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic",
  prompt = "#emo__qingyan",
  interaction = function (self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames(skel.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names}
  end,
  expand_pile = "emo__deduce",
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and player:getPileNameOfId(to_select) == "emo__deduce"
  end,
  view_as = function (self, player, cards)
    if self.interaction.data ~= nil and #cards == 1 then
      local card = Fk:cloneCard(self.interaction.data)
      card:addSubcards(cards)
      card.skillName = self.name
      return card
    end
  end,
  after_use = function (self, player, use)
    local room = player.room
    room:loseHp(player, 1, skel.name)
    if not player.dead then
      player:drawCards(math.max(1, player:getLostHp()), skel.name)
    end
  end,
  times = function (self, player)
    return 1 - player:usedEffectTimes(self.name, Player.HistoryTurn)
  end,
  enabled_at_play = function(self, player)
    return player:usedEffectTimes(self.name, Player.HistoryTurn) == 0 and #player:getPile("emo__deduce") > 0
  end,
  enabled_at_response = function(self, player)
    if player:usedEffectTimes(self.name, Player.HistoryTurn) == 0 and #player:getPile("emo__deduce") > 0 then
      local all_names = Fk:getAllCardNames("b")
      return #player:getViewAsCardNames(skel.name, all_names) > 0
    end
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return #player:getPile("emo__deduce") == 0 and target == player and player:hasSkill(skel.name) and player.phase == Player.Play
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__qingyan-put:::"..math.max(1, player:getLostHp()), })
  end,
  on_use = function (self, event, target, player, data)
    player:addToPile("emo__deduce", player.room:getNCards(math.max(1, player:getLostHp())), true, skel.name)
  end,
})

Fk:loadTranslationTable{
  ["emo__qingyan"] = "倾演",
  [":emo__qingyan"] = "出牌阶段开始时，若你没有“绎”，你可以将牌堆顶X张牌置于武将牌上称为“绎”；每回合限一次，你可以将“绎”当任意基本牌使用或打出，结算后失去一点体力并摸X张牌（X为你的已损体力值且至少为1）。",

  ["emo__deduce"] = "绎",
  ["#emo__qingyan"] = "你可以将“绎”当任意基本牌使用或打出",
  ["#emo__qingyan-put"] = "倾演:你可以将牌堆顶 %arg 张牌置于武将牌上称为“绎”",
}

return skel
